package com.hcs.chenyu.welcome;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.hcs.chenyu.MainActivity;
import com.hcs.chenyu.R;

public class view extends AppCompatActivity {
    private Button mwbtn;
    private ImageView welcome_gif;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        mwbtn = findViewById(R.id.wbtn);
        mwbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(10);
            }
        });
        initview();
        ImageView welcome_gif = (ImageView) findViewById(R.id.welcome_gif);
        Glide.with(this).load(R.drawable.myselfname).into(welcome_gif);
    }

    private void initview() {
        handler.sendEmptyMessageDelayed(6,1000);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage( Message msg) {//添加handler的消息等待方法来实现倒计时的显示
            super.handleMessage(msg);
            if(msg.what==6){
                mwbtn.setText("跳过(6)");
                handler.sendEmptyMessageDelayed(5,1000);
            }
            else if(msg.what==5){
                mwbtn.setText("跳过(5)");
                handler.sendEmptyMessageDelayed(4,1000);
            }
            else if(msg.what==4){
                mwbtn.setText("跳过(4)");
                handler.sendEmptyMessageDelayed(3,1000);
            }
            else if(msg.what==3){
                mwbtn.setText("跳过(3)");
                handler.sendEmptyMessageDelayed(2,1000);
            }
            else if(msg.what==2){
                mwbtn.setText("跳过(2)");
                handler.sendEmptyMessageDelayed(1,1000);
            }
            else if(msg.what==1){
                mwbtn.setText("跳过(1)");
                handler.sendEmptyMessageDelayed(0,1000);
            }
            else if(msg.what==0){
                mwbtn.setText("跳过(0)");
                handler.sendEmptyMessageDelayed(10,1000);
            }
            else if(msg.what==10){
                Intent intent = new Intent(view.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    };
//申请权限
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
       initview();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(6);
        handler.removeMessages(5);
        handler.removeMessages(4);
        handler.removeMessages(3);
        handler.removeMessages(2);
        handler.removeMessages(1);
        handler.removeMessages(0);
    }
}